<?php

namespace app\admin\controller;

use app\api\controller\Myshopify;
use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * shopify导入订单管理
 *
 * @icon fa fa-circle-o
 */
class Feditionordershopifyc extends Backend
{

    /**
     * Feditionordershopifyc模型对象
     * @var \app\admin\model\Feditionordershopifyc
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Feditionordershopifyc;
        $this->view->assign("statusList", $this->model->getStatusList());

        $reportmonth = $this->request->param('reportmonth');
        $this->assignconfig('reportmonth', $reportmonth);
        $type = $this->request->param('type');
        $this->assignconfig('type', $type);

        $monthtype = $this->request->param('monthtype');
        $this->assignconfig('monthtype', $monthtype);
        $monthids = $this->request->param('monthids');
        $this->assignconfig('monthids', $monthids);
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $where2 = array();
            $reportmonth = $this->request->param('reportmonth');
            if(!empty($reportmonth)){
                $reportmonth = Db::name('fedition_reportmonth')->where('id',$reportmonth)->find();
                if (!empty($reportmonth)) {
                    $startdate = $reportmonth['timedate'];
                    $enddate = mktime(23,59,59,date('m',$reportmonth['timedate']),date('t',$reportmonth['timedate']),date('Y',$reportmonth['timedate']));

                    $where2['created_at'] = array(array('egt',$startdate),array('elt',$enddate));
                    $where2['accounturl'] = $reportmonth['accounturl'];
                }
            }
            $where3 = array();
            //默认支付成功
            $type = $this->request->param('type');
            if (!empty($type)) {
                $where3['financial_status'] = array('in','paid,partially_refunded');
                $where3['fulfillment_status']  = 'fulfilled';
            }
            if($type == 1){
                $where3['istransfer'] = array('in','0,3');
            }
            if($type == 2){
                $where3['istransfer'] = array('in','1,5');
                //默认显示已付款已发货的订单
                $where3['fulfillment_status'] = 'fulfilled';
            }

            $monthtype = $this->request->param('monthtype');
            $this->assignconfig('monthtype', $monthtype);
            $monthids = $this->request->param('monthids');
            $this->assignconfig('monthids', $monthids);

//            店铺业绩页面显示订单
            $where4 = array();
            if (!empty($monthtype) && !empty($monthids)) {
               $reportmonth = Db::name('fedition_reportmonth')->where('id',$monthids)->find();
                if (!empty($reportmonth)) {
                    $startdate = $reportmonth['timedate'];
                    $enddate = mktime(23,59,59,date('m',$reportmonth['timedate']),date('t',$reportmonth['timedate']),date('Y',$reportmonth['timedate']));
                    $where4['created_at'] = array(array('egt',$startdate),array('elt',$enddate));
                    $where4['accounturl'] = $reportmonth['accounturl'];
                    $where4['financial_status'] = array('in','paid,partially_refunded');
                    // $where4['fulfillment_status']  = 'fulfilled';
                }
                switch ($monthtype) {
                    case '1':
                        break;
                    case '2':
                        $where4['istransfer'] = array('in','1,5');
                        break;
                    case '3':
                        $where4['istransfer'] = array('in','0,3');
                        break;
                    case '4':
                        $where4['ismaterial'] = array('in','1,4');
                        break;
                    case '5':
                        $where4['ismaterial'] = array('in','0,3');
                        break;
                    default:
                }
            }

            $list = $this->model
                ->where($where)
                ->where($where2)
                ->where($where3)
                ->where($where4)
                ->order($sort, $order)
                ->paginate($limit);
            $lists = $list->items();
            if($lists){
                foreach ($lists as $k=>$v){
                    $accounturl = Db::name('fedition_accounturl')->where('id', '=', $v['accounturl'])->find();
                    if($accounturl){
                        $lists[$k]['accounturl_name'] = $accounturl['name'];
                    }else{
                        $lists[$k]['accounturl_name'] = '店铺名称已删除';
                    }
                    //运单号
//                    $orderfulfillments = Db::name('fedition_orderfulfillments')->where('order_id',$v['id'])->find();
//                    if (!empty($orderfulfillments)) {
//                        $lists[$k]['tracking_company'] = $orderfulfillments['tracking_company'];
//                        $lists[$k]['tracking_number'] = $orderfulfillments['tracking_number'];
//                    } else {
//                        $lists[$k]['tracking_company'] = '';
//                        $lists[$k]['tracking_number'] = '';
//                    }
                }
            }
            $Statistics = array();
            $Statistics = $this->model
                ->where($where)
                ->where($where2)
                ->where($where3)
                ->where($where4)
                ->field('sum(total_price) as zsum,sum(freightmj) as freightmj,sum(material) as material,sum(countnum) as countnum')
                ->find();
            $Statistics['zsum'] =  round($Statistics['zsum'],2); //10.46
            $Statistics['zfreightmj'] =  round($Statistics['freightmj'],2);
            $Statistics['zmaterial'] =  round($Statistics['material'],2);
            $Statistics['zcountnum'] =  $Statistics['countnum'];
            $result = array("total" => $list->total(), "rows" => $lists, "Statistics" => $Statistics);
            return json($result);
        }
        return $this->view->fetch();
    }

}
